package com.csdig.cms.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.csdig.cms.dao.CmsSettingDAO;
import com.csdig.cms.model.CmsSetting;
import com.csdig.db.DBUtils;
import com.csdig.db.model.Pagination;
import com.csdig.db.model.Sql;

@Repository
public class CmsSettingDAOImpl implements CmsSettingDAO {
	

	@Override
	public void add(CmsSetting vo) throws Exception {
	    Sql sql = new Sql("insert into cms_setting(name,description,value)");
	    sql.append("values(:name,:description,:value)");
	   	sql.set("name",vo.getName());
	   	sql.set("description",vo.getDescription());
	   	sql.set("value",vo.getValue());
	    DBUtils.exeUpdate(sql);
	}

	@Override
	public void delete(String name) throws Exception {
	    String sql = "delete from cms_setting where name=?";
	    DBUtils.exeUpdate(sql, new Object[] {name });
	}

	@Override
	public Pagination<CmsSetting> findByCondition(CmsSetting vo, int pageNo,
			int pageSize) throws Exception{
	    Sql sql=new Sql("select * from cms_setting where 1=1 ");
	    ///add condtion here  
	    return DBUtils.queryPagination(sql, null, pageNo, pageSize,CmsSetting.class);
	}

	@Override
	public List<CmsSetting> findByCondition(CmsSetting vo) throws Exception{
	    Sql sql=new Sql("select * from cms_setting where 1=1 ");
	    ///add condtion here 
	    return DBUtils.queryBeanList(sql, CmsSetting.class);
	}

	@Override
	public CmsSetting findById(java.lang.String id) throws Exception {
	    String sql = "select * from cms_setting where name=?";
	    return DBUtils.queryBean(sql,CmsSetting.class,id);
	}

	@Override
	public List<CmsSetting> listAll() throws Exception {
	    return findByCondition(null);
	}

	@Override
	public void update(CmsSetting vo) throws Exception {
	    String sql = "update cms_setting set description=?,value=?  where name=?";
	    DBUtils.exeUpdate(sql,new Object[]{vo.getDescription(),vo.getValue(),vo.getName()});
	}

	@Override
	public List<CmsSetting> findAllFromCache() throws Exception {
		return findByCondition(null);
	}
    
}
